<template>
    <div class="content">
        <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
            <el-button type="primary" @click="handleOpen('add')">新增FAQ</el-button>
        </div>
        <!--搜索表单-->
        <el-table :data="tableData" row-key="id" height="100%" stripe :border="true">
            <el-table-column prop="id" label="ID" fixed="left" width="50"></el-table-column>
            <el-table-column prop="question" label="问题" />
            <el-table-column prop="answer" label="答案" :show-overflow-tooltip="true" />
            <el-table-column prop="order" label="排序"></el-table-column>
            <el-table-column label="状态">
                <template #default="scope">
                    <el-switch v-model="scope.row.is_active" @change="setShow(scope.row)" :active-value="1"
                        :inactive-value="0"></el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="created_at" label="创建时间"></el-table-column>
            <el-table-column label="操作" fixed="right" width="300">
                <template #default="scope">
                    <el-button type="primary" icon="Edit" @click="handleOpen('edit', scope.row)">
                        编辑
                    </el-button>
                    <el-button type="danger" icon="DeleteFilled" @click="delInfo(scope.row.id)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-divider border-style="none" />
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
            :current-page="curPage" :page-size="pageSize" layout="total, sizes,prev, pager, next, jumper"
            :total="total"></el-pagination>
        <Drawer ref="infoDrawer" @doneSave="getTableInfo" />
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import Drawer from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox, TabPaneName } from "element-plus";
import { changeStatusApi, delApi, editApi, getListAPi } from "@/api/carfilm/faq";
const total = ref(0);
const curPage = ref(1);
const pageSize = ref(20);
const departmentOption = ref<any[]>([]);
const tableData = ref([]);
const infoDrawer = ref<any>(null);
onMounted(() => {
    getTableInfo();
});
const handleSizeChange = (val: number) => {
    pageSize.value = val;
    getTableInfo();
};
const handleCurrentChange = (val: number) => {
    curPage.value = val;
    getTableInfo();
};
function getTableInfo() {
    getListAPi({
        page: curPage.value,
        limit: pageSize.value,
    }).then((res) => {
        tableData.value = res.data.list;
        total.value = res.data.total;
    });
}

function delInfo(id: number) {
    ElMessageBox.confirm("确认要删除该问题吗?", "警告！", {
        confirmButtonText: "确认删除",
        cancelButtonText: "取消",
        type: "warning",
    })
        .then(() => {
            delApi(id).then((res: any) => {
                getTableInfo();
                ElMessage({
                    type: "success",
                    message: "删除成功！",
                });
            });
        })
        .catch(() => {
            ElMessage({
                type: "info",
                message: "删除已取消",
            });
        });
}

const setShow = (row: any) => {
    changeStatusApi(row.id, row.is_active).then((res: any) => {
        if (res.code == 1) {
            ElMessage({
                type: "success",
                message: "更新成功！",
            });
        }
        getTableInfo();
    });
};

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(type: string, row?: any) {
    infoDrawer.value.open(type, row);
}
defineExpose({
    getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
    .el-tabs__new-tab {
        width: auto !important;
    }
}

.table-box {
    height: calc(100% - 200px);
}
</style>
